import os
from src.formats.ReadZipFiles import ReadZipFile
from src.formats.ESRIBundle.MapCache import *


def getSrcPath():
    return os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))

# tpk二维地图打包文件瓦片管理类
class ESRITpkManager:
    def __init__(self, filePath):
        # print('init TilesManager')
        self.srcPath = getSrcPath()
        # 打包文件路径
        self.filePath = filePath
        # 打包文件zipFile对象,提供读取打包文件内文件的方法
        self.tilesFile = ReadZipFile(filePath)
        self.layerPath = 'v101/Layers'

        self.checkLayersDir()
        self.XMLStringPath = self.layerPath + '/conf.xml'
        self.CDIStringPath = self.layerPath + '/conf.cdi'
        # important


        self.tileLayerInfo = self.readTileLayerInfo()  # XML数据与DCI数据
        self.WMTSCapabilities = self.readWMTSCapabilities() # 'src/conf/wmts/getcapabilities.xml'
        self.TMSRootXML = self.readTMSRootXML() # 'src/conf/tms/root.xml'
        self.TMSBasemapXML = self.readTMSBasemapXML() # 'src/conf/tms/basemap.xml'
        self.TMSTIlemapserviceXML = self.readTMSTilemapserviceXML()  # 'src/conf//tms/tilemapservice.xml'

    # 检查layers所在目录
    def checkLayersDir(self):
        if not self.tilesFile.isdir(self.layerPath):
            self.layerPath = 'v101/layers'
            if not self.tilesFile.isdir(self.layerPath):
                self.layerPath = 'v101/Layer'
                if not self.tilesFile.isdir(self.layerPath):
                    self.layerPath = 'v101/图层'
                    if not self.tilesFile.isdir(self.layerPath):
                        print('检测到可能存在中文文件夹，请检查并修改为英文')

    def readTileLayerInfo(self):
        xml = self.tilesFile.readSubFile(self.XMLStringPath)
        cdi = self.tilesFile.readSubFile(self.CDIStringPath)
        return CacheInfo.ReadFromXMLString(xml, cdi)

    # 直接返回图片数据
    def getTileFromLRC(self, level, row, column):
        bundlPre = judgeSubBundle(row, column)
        bundlePath = self.layerPath + '/_alllayers/L' + str(int(level)).zfill(2) + '/' + bundlPre + '.bundle'
        bundlxPath = self.layerPath + '/_alllayers/L' + str(int(level)).zfill(2) + '/' + bundlPre + '.bundlx'


        bundleFile = self.tilesFile.readSubFile(bundlePath)
        bundlxFile = self.tilesFile.readSubFile(bundlxPath)

        img = explodeTileInBundleWithBundlxFromBytesV1(row, column, bundlPre, bundleFile, bundlxFile,
                                                     self.tileLayerInfo.cacheStorageInfo.packetSize)

        return {
            "level": level,
            "row": row,
            "column": column,
            "imgData": img
        }

    def readWMTSCapabilities(self):
        path = self.srcPath + '/conf/wmts/getcapabilities.xml'
        with open(path, encoding='utf-8') as f:
            obj = f.read()
            return obj

    def readTMSRootXML(self):
        path = self.srcPath + '/conf/tms/root.xml'
        with open(path, encoding='utf-8') as f:
            obj = f.read()
            return obj

    def readTMSBasemapXML(self):
        path = self.srcPath + '/conf/tms/basemap.xml'
        with open(path, encoding='utf-8') as f:
            obj = f.read()
            return obj

    def readTMSTilemapserviceXML(self):
        path = self.srcPath + '/conf/tms/tilemapservice.xml'
        with open(path, encoding='utf-8') as f:
            obj = f.read()
            return obj
